package com.abc.recycling.controller;

import com.abc.recycling.domain.Evaluation;
import com.abc.recycling.service.EvaluationService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@Controller
@RequestMapping("/evaluation")
@RequiredArgsConstructor
public class EvaluationController {

    private final EvaluationService evaluationService;
    
    /**
     * 评价列表页面
     */
    @GetMapping("/list")
    public String list(Authentication authentication, Model model) {
        String role = authentication.getAuthorities().iterator().next().getAuthority();
        Long userId = Long.parseLong(authentication.getName());
        
        List<Evaluation> evaluations;
        if ("ROLE_COLLECTOR".equals(role)) {
            // 回收员查看自己收到的评价
            evaluations = evaluationService.getCollectorEvaluations(userId);
            // 获取评分分布
            List<Integer> distribution = evaluationService.getCollectorRatingDistribution(userId);
            // 获取平均评分
            Double averageRating = evaluationService.getCollectorAverageRating(userId);
            
            model.addAttribute("distribution", distribution);
            model.addAttribute("averageRating", averageRating);
        } else {
            // 用户查看自己发出的评价
            evaluations = evaluationService.getUserEvaluations(userId);
        }
        
        model.addAttribute("evaluations", evaluations);
        model.addAttribute("role", role);
        return "evaluation/list";
    }
    
    /**
     * 评价详情页面
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable Long id, Model model) {
        Evaluation evaluation = evaluationService.getEvaluation(id);
        model.addAttribute("evaluation", evaluation);
        return "evaluation/detail";
    }
    
    /**
     * 创建评价
     */
    @PostMapping("/create")
    @ResponseBody
    public Evaluation create(@RequestBody Evaluation evaluation, Authentication authentication) {
        Long userId = Long.parseLong(authentication.getName());
        evaluation.setUserId(userId);
        return evaluationService.createEvaluation(evaluation);
    }
    
    /**
     * 回复评价
     */
    @PostMapping("/{id}/reply")
    @PreAuthorize("hasRole('COLLECTOR')")
    @ResponseBody
    public Evaluation reply(@PathVariable Long id, @RequestParam String reply) {
        return evaluationService.replyEvaluation(id, reply);
    }
    
    /**
     * 搜索评价
     */
    @GetMapping("/search")
    @ResponseBody
    public List<Evaluation> search(@RequestParam(required = false) Long userId,
                                 @RequestParam(required = false) Long targetId,
                                 @RequestParam(required = false) Integer status,
                                 @RequestParam(required = false) Double minRating,
                                 @RequestParam(required = false) Double maxRating) {
        return evaluationService.searchEvaluations(userId, targetId, status, minRating, maxRating);
    }
    
    /**
     * 获取回收员评分统计
     */
    @GetMapping("/collector/{id}/stats")
    @ResponseBody
    public Object getCollectorStats(@PathVariable Long id) {
        Double averageRating = evaluationService.getCollectorAverageRating(id);
        List<Integer> distribution = evaluationService.getCollectorRatingDistribution(id);
        
        Map<String, Object> result = new HashMap<>();
        result.put("averageRating", averageRating);
        result.put("distribution", distribution);
        return result;
    }
    
    /**
     * 检查订单是否已评价
     */
    @GetMapping("/check/{orderId}")
    @ResponseBody
    public boolean checkOrderEvaluation(@PathVariable Long orderId) {
        return evaluationService.isOrderEvaluated(orderId);
    }
} 